package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.entity.Address;
import net.xdclass.enums.AddressStatusEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.mapper.AddressMapper;
import net.xdclass.model.LoginUser;
import net.xdclass.request.AddressRequest;
import net.xdclass.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电商-公司收发货地址表 服务实现类
 * </p>
 *
 * @author lixin
 * @since 2024-08-14
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    private final AddressMapper addressMapper;

    /**
     * 查找地址详情
     * @param id
     * @return
     */
    @Override
    public AddressVO detail(int id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        Address address = addressMapper.selectOne(new QueryWrapper<Address>().eq("id", id).eq("user_id", loginUser.getId()));
        if (address == null){
            return null;
        }

        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(address, addressVO);
        return addressVO;
    }

    /**
     * 新增收货地址
     * @param addressRequest
     */
    @Override
    public void add(AddressRequest addressRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        Address address = new Address();
        address.setCreateTime(LocalDateTime.now());
        address.setUserId(loginUser.getId());
        BeanUtils.copyProperties(addressRequest, address);

        //判断是否有默认收货地址
        if (address.getDefaultStatus() == 1){
            //查找数据库是否有收货地址
            Address defaultAddress = addressMapper.selectOne(new QueryWrapper<Address>().eq("user_id", loginUser.getId()).eq("default_status", AddressStatusEnum.DEFAULT_STATUS.getStatus()));

            if (defaultAddress != null){
                //修改为非默认收货地址
                defaultAddress.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
                addressMapper.update(defaultAddress, new QueryWrapper<Address>().eq("id", defaultAddress.getId()));
            }
        }

        int rows = addressMapper.insert(address);

        log.info("新增收货地址: rows={}, data={}", rows, address);
    }

    /**
     * 根据id删除收货地址
     * @param addressId
     * @return
     */
    @Override
    public int del(int addressId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        int rows = addressMapper.delete(new QueryWrapper<Address>().eq("id", addressId).eq("user_id", loginUser.getId()));
        return rows;
    }

    /**
     * 查询用户全部收货地址
     * @return
     */
    @Override
    public List<AddressVO> listUserAllAddress() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<Address> addressList = addressMapper.selectList(new QueryWrapper<Address>().eq("user_id", loginUser.getId()));

        List<AddressVO> addressVOList = addressList.stream().map(address -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(address, addressVO);
            return addressVO;
        }).collect(Collectors.toList());

        return addressVOList;
    }

}
